﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using Sell.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sell.Services.Phrases
{
    public class PhrasesServices : IPhrasesServices
    {
        public IPhraseRepository PhraseRepo { get; }
        public PhrasesServices(IPhraseRepository _PhraseRepo)
        {
            PhraseRepo= _PhraseRepo;
        }
        /// <summary>
        /// 销售合同新增
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddPhrase(ERP.Domain.SalesManagement.Phrase phrase)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "销售合同添加成功";
            var list = await PhraseRepo.AddAsync(phrase);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 销售合同删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeletePhrase(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                //根据ID获取销售合同信息
                var Ph = await PhraseRepo.GetModelAsync(id);
                if (Ph == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "销售合同不存在";
                    return res;
                }
                //将IsDelete状态设置为true
                Ph.IsDelete = true;
                //更新销售合同信息
                var result = await PhraseRepo.Update(Ph);
                res.Code = EnumCode.Ok;
                res.Message = "销售合同删除成功";
                res.Data = result;

            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"删除销售合同:{ex.Message}";
            }
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 修改销售合同
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdatePhrase(ERP.Domain.SalesManagement.Phrase phrase)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "销售合同修改成功";
            var list = await PhraseRepo.Update(phrase);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 修改审核状态
        /// </summary>
        /// <param name="id">销售合同ID</param>
        /// <param name="reviewStatus">审核状态 (0-未审核, 1-已审核)</param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateReviewStatus(int id, int reviewStatus)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                // 根据ID获取销售合同信息
                var phrase = await PhraseRepo.GetModelAsync(id);
                if (phrase == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "销售合同不存在";
                    return res;
                }

                // 验证审核状态值是否有效
                if (reviewStatus < 0 || reviewStatus > 1)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "审核状态值无效，有效值：0-未审核, 1-已审核";
                    return res;
                }

                // 更新审核状态
                phrase.Review = reviewStatus;
                var result = await PhraseRepo.Update(phrase);
                
                res.Code = EnumCode.Ok;
                res.Message = reviewStatus == 0 ? "未审核" : "已审核";
                res.Data = result;
            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"修改审核状态失败: {ex.Message}";
            }
            return await Task.FromResult(res);
        }
    }
}
